<template>
	<div class="login-from">
		<header>
			<h1>房屋管理系统</h1>
			<p>house's administration system</p>
		</header>
		<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="60px" class="demo-ruleForm">
			<el-form-item label="账号:" prop="a_account">
				<el-input v-model="ruleForm.a_account" @keyup.enter.native="submitForm('ruleForm')"></el-input>
			</el-form-item>
			<el-form-item label="密码:" prop="a_password">
				<el-input type="password" v-model="ruleForm.a_password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import "../style/login.css" 
	import config from '../config/userconf.js' 
  export default {
    data() {
      return {
        ruleForm: {
          a_account: '',
          a_password: ''
        },
        rules: {
          a_account: [
            { required: true, message: '请输入用户账号', trigger: 'blur' }
          ],
          a_password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, message: '密码不少于6位', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            config.login(this.ruleForm).then((res)=>{
              if (res.data.code ==0) {
                this.$router.push({name:'adminaccountlist'})
                window.localStorage.username = this.ruleForm.a_account
                window.localStorage.Token = res.data.data
              }
            }).catch((e)=>{
              console.log(e)
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
	  mounted (){
        this.$nextTick(function(){
           this.ruleForm.a_account =  window.localStorage.username?window.localStorage.username:''
        })
    },
  }
</script>
